﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FacturIT.Service.Repositories;

namespace FacturIT.Service.User
{
    internal class UserFactory
    {
        private static UserFactory instance = null;
        public static UserFactory Instance
        {
            get
            {
                if (instance == null)
                    instance = new UserFactory();
                return instance;
            }
        }

        private UserService UserService;
        public UserService GetUserService()
        {
            if (UserService == null)
                UserService = new UserService();

            return UserService;
        }
    }
    internal class UserService
    {
        private IRepository<BLL.Model.User.User, int> repository;
        private IRepository<BLL.Model.User.User, int> Repository
        {
            get
            {
                if (repository == null)
                    repository = new UserRepository();

                return repository;
            }
        }

        internal UserDTO AddUser(string fname, string lname, string uname, string street, string pc, string city, string country, string phone, string btw)
        {
            UserDTO User = Convert(Repository.Add(new BLL.Model.User.User()
            {
                Firstname = fname,
                Lastname = lname,
                Username = uname,
                Street = street,
                PostalCode = pc,
                City = city,
                Country = country,
                Phonenumber = phone,
                BTW = btw
            }));
            return User;
        }

        internal UserDTO ReadUserFromDatabase(string username, string password)
        {
            return Convert(((UserRepository)Repository).ReadUserFromDatabase(username, password));
        }
        #region HelperMethods

        private static UserDTO Convert(BLL.Model.User.User User)
        {
            return new UserDTO(User);
        }

        internal UserDTO ReadUser(int userID)
        {
            return Convert(((UserRepository)Repository).ReadUser(userID));
        }
        #endregion
    }
}